Skip to content

Conversation

@YushengZhao
Copy link

Description

This PR adds support for the Gated Linear Attention (GLA) operator in the GGML CANN backend. GLA is widely used in efficient attention mechanisms (e.g., RWKV, Linear Transformer variants, etc.), which leverage gating signals and state accumulation to significantly reduce computational complexity while preserving strong modeling capacity.

Summary of Changes:

  • Registered GGML_OP_GATED_LINEAR_ATTN in ggml/src/ggml-cann/ggml-cann.cpp and bound it to a newly implemented function ggml_cann_gated_linear_attn.
  • Implemented the core forward logic of ggml_cann_gated_linear_attn in ggml/src/ggml-cann/aclnn_ops.cpp, using ACLNN primitives such as Repeat, Mul, Add, and Mv to compose the GLA computation.
  • Supports batched multi-head GLA with input tensor layout (C, H, T, B), where:
    • C = H * D (total channel dimension),
    • T = B * L (flattened batch × sequence length),
    • consistent with GGML’s internal memory layout conventions.
  • Accepts learnable gate g and recurrent state s as additional inputs, enabling joint state update and output generation in a single pass.

Testing

Build with CANN backend enabled:

cmake -B build -DGGML_CANN=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j

Run GLA-specific backend test (requires adding a test case for GATED_LINEAR_ATTN in tests/test-backend-ops.cpp):

./bin/test-backend-ops test -b CANN0 -o GATED_LINEAR_ATTN
image

@github-actions github-actions bot added ggml changes relating to the ggml tensor library for machine learning Ascend NPU issues specific to Ascend NPUs labels Dec 6, 2025
@YushengZhao YushengZhao changed the title support gated linear attn CANN: support gated linear attn Dec 6, 2025
@ggerganov
Copy link
Member

Note that this operator might be removed at some point - see #17716 (comment)

@hipudding
Copy link
Collaborator

@YushengZhao Thank you for your contribution!

aclTensor* acl_s = ggml_cann_create_tensor(s, ne_s, nb_s, 2, ACL_FORMAT_ND, s_offset);
aclTensor* acl_s_new = ggml_cann_create_tensor(dst, ne_s, nb_s, 2, ACL_FORMAT_ND, (B * L * H * D) * nb_base + s_offset);
cann_copy(ctx, acl_s, acl_s_new);
for (int64_t l = 0; l < L; l++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contains a triple loop, which may lead to poor performance. Are there better design ideas to reduce the complexity of operator calls?

* @brief Computes the Gated Linear Attention for a ggml tensor using the CANN
* backend.
*
* @details ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this detailed section still not finished?

*/
void aclnn_sin(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst);

static void cann_copy(ggml_backend_cann_context& ctx, aclTensor* acl_src, aclTensor* acl_dst);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need call some inner functions, it's better to declare them here, just like aclnn_sin does.

@CISC
Copy link
Collaborator

CISC commented Dec 17, 2025

@hipudding In case you're wondering I'm the one canceling Server CI jobs, they are causing trouble, fix pending merge...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ascend NPU issues specific to Ascend NPUs ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants